home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / sbin / ndisgtk < prev    next >
Text File  |  2008-10-07  |  11KB  |  365 lines

  1. #!/usr/bin/env python
  2.  
  3. #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
  4. #                                                                             #
  5. # ndisgtk - A GTK frontend for the ndiswrapper wireless driver tool           #
  6. #                                                                             #
  7. # Copyright (C) 2005, Sam Pohlenz <retrix@internode.on.net>                   #
  8. # Copyright (C) 2007-2008 Julian Andres Klode <jak@jak-linux.org>             #
  9. #                                                                             #
  10. # This program is free software; you can redistribute it and/or               #
  11. # modify it under the terms of the GNU General Public License                 #
  12. # as published by the Free Software Foundation; either version 2              #
  13. # of the License, or (at your option) any later version.                      #
  14. #                                                                             #
  15. # This program is distributed in the hope that it will be useful,             #
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of              #
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
  18. # GNU General Public License for more details.                                #
  19. #                                                                             #
  20. # You should have received a copy of the GNU General Public License           #
  21. # along with this program; if not, write to the Free Software                 #
  22. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. #
  23. #                                                                             #
  24. #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
  25.  
  26. import sys
  27. import os
  28. import subprocess
  29. import re
  30.  
  31.  
  32. def getoutput(*cmd):
  33.     '''Like commands.getoutput, but uses subprocess'''
  34.     myproc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  35.     value  =  myproc.stdout.read()
  36.     retcode = myproc.wait()
  37.     return value, retcode
  38.  
  39. # Attempt to load GTK bindings
  40. try:
  41.     import pygtk
  42.     pygtk.require("2.0")
  43.     import gtk
  44.     import gtk.glade
  45.     import gobject
  46. except ImportError:
  47.     print "Failed to load GTK bindings. Please check your Gnome installation."
  48.     sys.exit(1)
  49.  
  50. # Internationalization
  51. import locale
  52. import gettext
  53. locale.setlocale(locale.LC_ALL, "")
  54. gettext.bindtextdomain("ndisgtk", "/usr/share/locale")
  55. gettext.textdomain("ndisgtk")
  56. gettext.install("ndisgtk", "/usr/share/locale", unicode=1)
  57. gtk.glade.bindtextdomain("ndisgtk", "/usr/share/locale")
  58. gtk.glade.textdomain("ndisgtk")
  59.  
  60. # Data directory
  61. DATA_DIR = "/usr/share/ndisgtk"
  62.  
  63. def error_dialog(message, parent = None):
  64.     """
  65.     Displays an error message.
  66.     """
  67.  
  68.     dialog = gtk.MessageDialog(parent = parent, type = gtk.MESSAGE_ERROR, buttons = gtk.BUTTONS_OK, flags = gtk.DIALOG_MODAL)
  69.     dialog.set_markup(message)
  70.  
  71.     result = dialog.run()
  72.     dialog.destroy()
  73.  
  74. class NdisGTK:
  75.     """
  76.     Main application class.
  77.     """
  78.  
  79.     def __init__(self, kde=False):
  80.         """
  81.         Initializes the application.
  82.         """
  83.  
  84.         # Setup glade and signals
  85.         self.signals = { "gtk_main_quit": gtk.main_quit,
  86.                          "on_install_driver_clicked": self.install_driver_open,
  87.                          "on_remove_driver_clicked": self.remove_driver,
  88.                          "on_driver_list_cursor_changed": self.cursor_changed,
  89.                          "install_dialog_close": self.install_dialog_close,
  90.                          "install_button_clicked": self.install_driver,
  91.                          "network_button_clicked": self.config_network,
  92.                          "help_button_clicked": self.show_help,
  93.                          "drag_motion": self.drag_motion,
  94.                          "drag_data_received": self.drag_data_received }
  95.  
  96.         self.widgets = gtk.glade.XML(DATA_DIR + "/ndisgtk.glade", domain="ndisgtk")
  97.         self.widgets.signal_autoconnect(self.signals)
  98.  
  99.         # Get handle to window
  100.         self.window = self.widgets.get_widget("ndiswrapper_main")
  101.  
  102.         # Load icon
  103.         icon_theme = gtk.icon_theme_get_default()
  104.         self.wifi_icon = icon_theme.load_icon('ndisgtk', 48, 0)
  105.         self.wifi_error_icon = icon_theme.load_icon('ndisgtk-error', 48, 0)
  106.         self.window.set_icon(self.wifi_icon)
  107.  
  108.         # Get handle to 'Remove Driver' button
  109.         self.remove_driver = self.widgets.get_widget("remove_driver")
  110.  
  111.         # Get handle to 'Install Driver' dialog
  112.         self.install_dialog = self.widgets.get_widget("install_dialog")
  113.         self.install_dialog.set_transient_for(self.window)
  114.  
  115.         # Get handle to file chooser
  116.         self.file_chooser = self.widgets.get_widget("filechooser")
  117.  
  118.         # Enable drag-and-drop
  119.         self.window.drag_dest_set(gtk.DEST_DEFAULT_DROP, [("text/plain", 0, 80)], gtk.gdk.ACTION_COPY)
  120.  
  121.         # Setup driver list
  122.         self.setup_driver_list()
  123.  
  124.         # Use KDE network admin?
  125.         self.kde = kde
  126.  
  127.         gtk.main()
  128.  
  129.     def setup_driver_list(self):
  130.         """
  131.         Sets up the driver list and list widget.
  132.         """
  133.  
  134.         # Initialize lists
  135.         self.driver_list = []
  136.         self.driver_list_store = gtk.ListStore(gtk.gdk.Pixbuf, gobject.TYPE_STRING)
  137.         self.driver_list_widget = self.widgets.get_widget("driver_list")
  138.  
  139.         # Set up columns
  140.         first = gtk.TreeViewColumn("icon", gtk.CellRendererPixbuf(), pixbuf = 0)
  141.         second = gtk.TreeViewColumn("desc", gtk.CellRendererText(), markup = 1)
  142.  
  143.         self.driver_list_widget.append_column(first)
  144.         self.driver_list_widget.append_column(second)
  145.  
  146.         # Set list model for widget
  147.         self.driver_list_widget.set_model(self.driver_list_store)
  148.  
  149.         # Load the list of drivers
  150.         self.get_driver_list()
  151.  
  152.     def get_driver_list(self):
  153.         """
  154.         Gets the list of drivers from ndiswrapper.
  155.         """
  156.  
  157.         # Clear driver list
  158.         self.driver_list_store.clear()
  159.         self.driver_list = []
  160.  
  161.         # Run the ndiswrapper list command
  162.         output,retcode = getoutput("ndiswrapper", "-l")
  163.  
  164.         if "WARNING" in output:
  165.             error_dialog(_("Unable to see if hardware is present."), self.window)
  166.  
  167.         if ": driver" in output or ": invalid" in output:
  168.             # Newer ndiswrapper versions
  169.             # Drivers found
  170.             output = output.splitlines()
  171.  
  172.             for i in range(0, len(output)):
  173.                 line = output[i]
  174.                 if len(output) > i+1: line2 = output[i+1]
  175.                 else: line2=""
  176.  
  177.                 if "present" in line2: hardware_present = _("Yes")
  178.                 else: hardware_present = _("No")
  179.  
  180.                 # Get driver name
  181.                 p = re.compile(".*:")                        # match up to first tab
  182.                 driver_name = p.search(line).group()[:-1].strip()    # strip trailing space
  183.  
  184.                 # Add to list
  185.                 if "installed" in line:
  186.                     self.driver_list.append(driver_name)
  187.                     self.driver_list_store.append([self.wifi_icon,
  188.                         _("<b>%s</b>\nHardware present: %s") % (driver_name, hardware_present)])
  189.                 elif "invalid" in line:
  190.                     self.driver_list.append(driver_name)
  191.                     self.driver_list_store.append([self.wifi_error_icon,
  192.                         _("<b>%s</b>\nInvalid Driver!") % driver_name])
  193.  
  194.         elif "installed" in output or "Installed" in output or "invalid" in output:
  195.             # Drivers found
  196.             output = output.splitlines()
  197.             for i in range(1, len(output)):
  198.                 line = output[i]
  199.  
  200.                 if "hardware" in line: hardware_present = _("Yes")
  201.                 else: hardware_present = _("No")
  202.  
  203.                 # Get driver name
  204.                 p = re.compile(".*\\t")                        # match up to first tab
  205.                 driver_name = p.search(line).group()[:-1].strip()    # strip trailing space
  206.  
  207.                 # Add to list
  208.  
  209.                 if "installed" in line:
  210.                     self.driver_list.append(driver_name)
  211.                     self.driver_list_store.append([self.wifi_icon,
  212.                         _("<b>%s</b>\nHardware present: %s") % (driver_name, hardware_present)])
  213.                 elif "invalid" in line:
  214.                     self.driver_list.append(driver_name)
  215.                     self.driver_list_store.append([self.wifi_icon,
  216.                         _("<b>%s</b>\nInvalid Driver!") % driver_name])
  217.         else:
  218.             # No drivers installed
  219.             pass
  220.  
  221.     def drag_motion(self, window, context, x, y, time):
  222.         """
  223.         Called whenever a drag motion is made.
  224.         """
  225.  
  226.         context.drag_status(gtk.gdk.ACTION_COPY, time)
  227.         return True
  228.  
  229.     def drag_data_received(self, window, context, x, y, selection, info, timestamp):
  230.         """
  231.         Called when a file is dragged onto the main window.
  232.         """
  233.  
  234.         file = selection.get_text().strip()
  235.  
  236.         if file.startswith("file://"):
  237.             if file.endswith(".inf"):
  238.                 self.file_chooser.set_uri(file)
  239.                 self.install_driver_open()
  240.             else:
  241.                 error_dialog(_("Please drag an '.inf' file instead."), self.window)
  242.  
  243.         return True
  244.  
  245.     def show_help(self, *args):
  246.         """
  247.         Displays the help window.
  248.         Called when the 'Help' button is clicked.
  249.         """
  250.  
  251.         # TODO: Implement
  252.         error_dialog("Help", self.window)
  253.  
  254.     def config_network(self, *args):
  255.         """
  256.         Opens the network configuration tool.
  257.         """
  258.         try:
  259.             if self.kde:
  260.                 subprocess.Popen(["kcmshell", "kcm_knetworkconfmodule"])
  261.             else:
  262.                 subprocess.Popen(["network-admin"])
  263.         except OSError, e:
  264.             if e.errno == 2:
  265.                 error_dialog(_("Could not find a network configuration tool."))
  266.             else:
  267.                 raise e
  268.  
  269.     def install_driver_open(self, *args):
  270.         """
  271.         Opens the install driver dialog.
  272.         """
  273.  
  274.         self.install_dialog.show()
  275.  
  276.     def install_dialog_close(self, *args):
  277.         """
  278.         Closes the install driver dialog.
  279.         """
  280.  
  281.         self.install_dialog.hide()
  282.         return True;
  283.  
  284.     def install_driver(self, *args):
  285.         """
  286.         Installs a selected wireless driver.
  287.         Called when the install dialog's 'Install Driver' button is clicked.
  288.         """
  289.  
  290.         inf_file = self.file_chooser.get_filename()
  291.  
  292.         if inf_file == None:
  293.             error_dialog(_("No file selected."), self.install_dialog)
  294.         elif not inf_file.lower().endswith(".inf"):
  295.             error_dialog(_("Not a valid driver .inf file."), self.install_dialog)
  296.         else:
  297.             # Attempt to install driver
  298.             output, retcode = getoutput("ndiswrapper", "-i", inf_file)
  299.  
  300.             # Attempt to detect errors
  301.             if "already" in output:
  302.                 #driver_name = output.split()[0]
  303.                 error_dialog(_("Driver is already installed."), self.install_dialog)
  304.             elif retcode != 0:
  305.                 error_dialog(_("Error while installing.") , self.install_dialog)
  306.             else:
  307.                 # Assume driver installed successfully. Set up and reload module
  308.                 subprocess.call(["ndiswrapper",  "-ma"])
  309.                 subprocess.call(["modprobe", "-r", "ndiswrapper"])
  310.                 out, ret = getoutput("modprobe", "ndiswrapper")
  311.                 if ret != 0:
  312.                     error_text = _("Module could not be loaded. Error was:\n\n<i>%s</i>\n")
  313.                     if "not found" in out:
  314.                         error_text += _("Is the ndiswrapper module installed?")
  315.                     error_dialog(error_text % out, self.install_dialog)
  316.  
  317.                 self.get_driver_list()
  318.                 self.install_dialog_close()
  319.  
  320.     def remove_driver(self, *args):
  321.         """
  322.         Removes a driver after asking for confirmation.
  323.         Called when the 'Remove Driver' button is clicked.
  324.         """
  325.  
  326.         # Get the first selected driver
  327.         cursor = self.driver_list_widget.get_cursor()[0][0]
  328.         driver_name = self.driver_list[cursor]
  329.  
  330.         # Get confirmation
  331.         confirm = gtk.MessageDialog(type = gtk.MESSAGE_WARNING, buttons = gtk.BUTTONS_YES_NO)
  332.         confirm.set_markup(_("Are you sure you want to remove the <b>%s</b> driver?") % driver_name)
  333.         result = confirm.run()
  334.  
  335.         if result == gtk.RESPONSE_YES:
  336.             # Remove driver
  337.             output,retcode = getoutput("ndiswrapper", "-e", driver_name)
  338.  
  339.             # Reload driver list
  340.             self.get_driver_list()
  341.  
  342.         # Destroy the confirmation dialog
  343.         confirm.destroy()
  344.  
  345.     def cursor_changed(self, *args):
  346.         """
  347.         Called when the currently selected driver changes.
  348.         """
  349.  
  350.         # Allow the 'Remove Driver' button to be clicked
  351.         self.remove_driver.set_sensitive(True)
  352.  
  353.  
  354. if __name__ == '__main__':
  355.     # Check for root privileges
  356.     if os.getuid() != 0:
  357.         error_dialog(_("Root or sudo privileges required!"))
  358.         sys.exit(1)
  359.  
  360.     # Parse options and load GUI
  361.     if "--kde" in sys.argv:
  362.         NdisGTK(kde=True)
  363.     else:
  364.         NdisGTK()
  365.